/*
 * Copyright 2011 group coding kata nantes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.coding.kata.nantes.bench.bowling;

import fr.coding.kata.nantes.bench.AbstractRunner;
import fr.coding.kata.nantes.bench.Couple;
import fr.coding.kata.nantes.bowling.commons.BowlingInterface;
import fr.coding.kata.nantes.utils.file.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Benchmark runner for the Bowling kata implementations.
 *
 * @author Sylvain Leduby
 * @since 1.0
 */
public class BowlingRunner extends AbstractRunner<BowlingInterface, String, Integer> {

    /**
     * Implementations to run.
     */
    private static final List<BowlingInterface> IMPLS;

    static {
        IMPLS = new ArrayList<BowlingInterface>();
        // add your implementation here
        IMPLS.add(new fr.coding.kata.nantes.bowling.christophe.Bowling());
        IMPLS.add(new fr.coding.kata.nantes.bowling.sylvain.Bowling());
        IMPLS.add(new fr.coding.kata.nantes.bowling.michel.Bowling());
    }

    public BowlingRunner() {
        super(IMPLS, getData());
    }

    /**
     * Extract the set of data from a file.
     *
     * @return The data for the test.
     */
    private static List<Couple<String, Integer>> getData() {
        final List<String> lines = readLines();

        final List<Couple<String, Integer>> dataset = new ArrayList<Couple<String, Integer>>();
        for (final String line : lines) {
            final String[] elem = StringUtils.split(line, ',');
            dataset.add(new Couple<String, Integer>(elem[0], Integer.valueOf(elem[1])));
        }

        return dataset;
    }

    private static List<String> readLines() {
        final File file = getFile();
        try {
            return FileUtils.readLines(file);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static File getFile() {
        try {
            return FileUtil.find("fr/coding/kata/nantes/bench/bowling/data.txt");
        } catch (final FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BowlingInterface init(final BowlingInterface target, final String input) {
        return target.with(input);
    }

}